/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mytdev.jfx.scene.control;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Control;
import javafx.scene.image.Image;

/**
 * A simple image chooser control.
 *
 * @author Yann D'Isanto
 */
public class ImageChooser extends Control {

    private static final int DEFAULT_IMAGE_HEIGHT = 64;

    private static final int DEFAULT_IMAGE_WIDTH = 64;

    private static final int DEFAULT_PADDING = 0;

    private static final Image DEFAULT_NO_SELECTION_IMAGE = new Image(
            ImageChooser.class.getResourceAsStream(
            "/com/mytdev/jfx/internal/scene/control/no-image.png"));

    /**
     * The displayed image width.
     */
    private final DoubleProperty imageWidth = new SimpleDoubleProperty(DEFAULT_IMAGE_WIDTH);

    /**
     * The displayed image height.
     */
    private final DoubleProperty imageHeight = new SimpleDoubleProperty(DEFAULT_IMAGE_HEIGHT);

    /**
     * The padding.
     */
    private IntegerProperty padding = new SimpleIntegerProperty(DEFAULT_PADDING);

    /**
     * The selected image.
     */
    private final ObjectProperty<Image> selectedImage = new SimpleObjectProperty<>();

    /**
     * The displayed image if no image is selected.
     */
    private final ObjectProperty<Image> noSelectionImage = new SimpleObjectProperty<>(DEFAULT_NO_SELECTION_IMAGE);

    /**
     * Default constructor
     */
    public ImageChooser() {
        // setup the CSS
        // the -fx-skin attribute in the CSS sets which Skin class is used
        this.getStyleClass().add(this.getClass().getSimpleName().toLowerCase());
    }

    /**
     * Return the path to the CSS file so things are setup right
     */
    @Override
    protected String getUserAgentStylesheet() {
        return this.getClass().getResource("/com/mytdev/jfx/internal/scene/control/" + this.getClass().getSimpleName() + ".css").toString();
    }

    /**
     * Returns the displayed image width property.
     *
     * @return the displayed image width property.
     */
    public DoubleProperty imageWidthProperty() {
        return imageWidth;
    }

    /**
     * Returns the displayed image width.
     *
     * @return the displayed image width.
     */
    public double getImageWidth() {
        return imageWidth.get();
    }

    /**
     * Sets the displayed image width.
     *
     * @param imageWidth the image width vamue to set.
     */
    public void setImageWidth(double imageWidth) {
        this.imageWidth.set(imageWidth);
    }

    /**
     * Returns the displayed image height property.
     *
     * @return the displayed image height property.
     */
    public DoubleProperty imageHeightProperty() {
        return imageHeight;
    }

    /**
     * Returns the displayed image height.
     *
     * @return the displayed image height.
     */
    public double getImageHeight() {
        return imageHeight.get();
    }

    /**
     * Sets the displayed image height.
     *
     * @param imageWidth the image height vamue to set.
     */
    public void setImageHeight(double imageHeight) {
        this.imageHeight.set(imageHeight);
    }

    /**
     * Returns the padding property.
     *
     * @return the padding property.
     */
    public IntegerProperty paddingProperty() {
        return padding;
    }

    /**
     * Returns the padding.
     *
     * @return the padding.
     */
    public int getPadding() {
        return padding.get();
    }

    /**
     * Sets the padding.
     *
     * @param padding the padding to set.
     */
    public void setPadding(int padding) {
        this.padding.set(padding);
    }

    /**
     * Returns the selected image property.
     *
     * @return the selected image property.
     */
    public ObjectProperty<Image> selectedImageProperty() {
        return selectedImage;
    }

    /**
     * Returns the selected image.
     *
     * @return the selected image or null if none.
     */
    public Image getSelectedImage() {
        return selectedImage.get();
    }

    /**
     * Sets the selected image.
     *
     * @param selectedImage the image to set selected. Can be <code>null</code>
     * (no selected image).
     */
    public void setSelectedImage(Image selectedImage) {
        this.selectedImage.set(selectedImage);
    }

    /**
     * Returns the no-selection image property.
     *
     * @return the no-selection image property.
     */
    public ObjectProperty<Image> noSelectionImageProperty() {
        return noSelectionImage;
    }

    /**
     * Returns the image displayed when no image is selected.
     *
     * @return the no-selection image.
     */
    public Image getNoSelectionImage() {
        return noSelectionImage.get();
    }

    /**
     * Sets the no-selection image.
     *
     * @param noSelectionImage the image to display when no image is selected.
     */
    public void setNoSelectionImage(Image noSelectionImage) {
        this.noSelectionImage.set(noSelectionImage);
    }
}
